package com.kawa.kw_pay;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

import androidx.annotation.NonNull;

import java.util.HashMap;
import java.util.Map;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry;

/** KwPayPlugin */
public class KwPayPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
  private MethodChannel channel;
  private Activity mActivity;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "kw_pay");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    Map<String,String> params = (Map<String, String>) call.arguments;
    switch (call.method){
      case "pay":
        handlerPay(params,result);
        break;
      default:
        result.notImplemented();
        break;
    }
  }
  
  private void handlerPay(Map<String, String> params, final Result result){
    String type = params.get("type");
    if (type == null){
      result.error("-1","请提交支付类型",null);
      return;
    }
    String mode = params.get("mode");
    if (mode == null || mode.equals("dev")){
      mode = "01";
    }else{
      mode = "00";
    }
    switch (type){
      case "upPay"://银联支付
        UPPayUtils.registerActivity(mActivity);
        Log.e("pay",params.get("orderId"));
        Log.e("pay",mode);
        UPPayUtils.startUPPay(params.get("orderId"), mode, new UPPayUtils.Callback() {
          @Override
          public void onResult(String _result, String data) {
            Map<String,String> map = new HashMap<>();
            map.put("result",_result);
//            map.put("data",data);
            result.success(map);
          }
        });
        break;
      default:
        break;
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }

  @Override
  public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
    mActivity = binding.getActivity();
    binding.addActivityResultListener(new PluginRegistry.ActivityResultListener() {
      @Override
      public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
        UPPayUtils.handlerResult(data);
        return false;
      }
    });
  }

  @Override
  public void onDetachedFromActivityForConfigChanges() {

  }

  @Override
  public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {

  }

  @Override
  public void onDetachedFromActivity() {

  }
}
